home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / overxms.exe / OVERXMS.ASM < prev    next >
Assembly Source File  |  1993-01-05  |  9KB  |  350 lines

  1. TITLE Turbo Pascal XMS support for loading overlays - By Wilbert van Leijen
  2. ; OVERXMS 1.1
  3. ; Bug fix by Arnold Bailey  [72020,136]   BIX abailey 1/03/93
  4. ;
  5. ;   XMS uses BX to return error codes. Most version will preserve the value
  6. ;   of BX if there is no error.
  7. ;
  8. ;   DRDOS 6 EMM386.SYS and HIDOS.SYS (equivalent to HIMEM.SYS) change
  9. ;   BX to zero if there is no error. The procedure UnitToXMS uses BX as an
  10. ;   offset counter. With DRDOS 6 it gets reset causing major crash.
  11. ;
  12. ;   Added PUSH BX and POP BX to preserve value.
  13.  
  14. PAGE 65, 132
  15. LOCALS @@
  16.  
  17. Data       SEGMENT Word Public
  18.            ASSUME  DS:Data
  19.  
  20. ;  XMS block move record
  21.  
  22. XmsMoveType STRUC
  23.            BlkSize     DD    ?
  24.            SrcHandle   DW    ?
  25.            SrcOffset   DD    ?
  26.            DestHandle  DW    ?
  27.            DestOffset  DD    ?
  28. XmsMoveType ENDS
  29.  
  30. ;  TP overlay manager record
  31.  
  32. OvrHeader  STRUC
  33.            ReturnAddr  DD    ?         ; Virtual return address
  34.            FileOfs     DD    ?         ; Offset into overlay file
  35.            CodeSize    DW    ?         ; Size of overlay
  36.            FixupSize   DW    ?         ; Size of fixup table
  37.            EntryPts    DW    ?         ; Number of procedures
  38.            CodeListNext DW   ?         ; Segment of next overlay
  39.            LoadSeg     DW    ?         ; Start segment in memory
  40.            Reprieved   DW    ?         ; Loaded in memory flag
  41.            LoadListNext DW   ?         ; Segment of next in load list
  42.            XmsOffset   DD    ?         ; Offset into allocated XMS block
  43.            UserData    DW    3 DUP(?)
  44. OvrHeader  ENDS
  45.  
  46. XmsDriver  DD      ?                   ; Entry point of XMS driver
  47. ExitSave   DD      ?                   ; Pointer to previous exit proc
  48. XmsMove    XmsMoveType <>
  49. OvrXmsHandle DW    ?                   ; Returned by XMS driver
  50.  
  51.            Extrn   PrefixSeg : Word
  52.            Extrn   ExitProc : DWord
  53.            Extrn   OvrResult : Word
  54.            Extrn   OvrCodeList : Word
  55.            Extrn   OvrDosHandle : Word
  56.            Extrn   OvrHeapOrg : Word
  57.            Extrn   OvrReadBuf : DWord
  58. Data       ENDS
  59.  
  60. Code       SEGMENT Byte Public
  61.            ASSUME  CS:Code
  62.            Public  OvrInitXMS
  63.  
  64. ovrIOError     EQU     -4
  65. ovrNoXMSDriver EQU     -7
  66. ovrNoXMSMemory EQU     -8
  67.  
  68. OvrXmsExit PROC
  69.  
  70. ; Release handle and XMS memory
  71.  
  72.         MOV    DX, [OvrXmsHandle]
  73.         MOV    AH, 10
  74.         CALL   [XmsDriver]
  75.  
  76. ; Restore pointer to previous exit procedure
  77.  
  78.         LES    AX, [ExitSave]
  79.         MOV    Word Ptr [ExitProc], AX
  80.         MOV    Word Ptr [ExitProc+2], ES
  81.         RETF
  82. OvrXmsExit ENDP
  83.  
  84. AllocateXms PROC
  85.  
  86. ;  Determine the size of the XMS block to allocate:
  87. ;  Walk the CodeListNext chain
  88. ;  Store the total codesize in DX:AX
  89.  
  90.         XOR    AX, AX
  91.         XOR    DX, DX
  92.         MOV    BX, [OvrCodeList]
  93. @@1:    ADD    BX, [PrefixSeg]
  94.         ADD    BX, 10h
  95.         MOV    ES, BX
  96.  
  97. ;** Ver 1.2 BugFix
  98.         MOV    CX, ES:[OvrHeader.CodeSize]
  99.         TEST   CX,1     ; Test for odd number of bytes
  100.         JZ     @@2
  101.         INC    CX       ; Make sure it's even number of bytes
  102. @@2:    ADD    AX, CX
  103. ;** Ver 1.2 Bug Fix
  104.  
  105.         ADC    DX, 0
  106.         MOV    BX, ES:[OvrHeader.CodeListNext]
  107.         OR     BX, BX
  108.         JNZ    @@1
  109.  
  110. ;  Obtain number of kilobytes to allocate
  111.  
  112.         MOV    BX, 1024
  113.         DIV    BX
  114.         XCHG   DX, AX
  115.         INC    DX
  116.  
  117. ;  Allocate the block
  118.  
  119.         MOV    AH, 9
  120.         CALL   [XmsDriver]
  121.         OR     AX, AX
  122.         JZ     @@3
  123.         MOV    [OvrXmsHandle], DX
  124. @@3:    RETN
  125. AllocateXms ENDP
  126.  
  127. ;  Function XmsReadFunc(OvrSeg : Word) : Integer; Far;
  128.  
  129. XmsReadFunc PROC
  130.  
  131. ;  Swap the code from XMS to the heap
  132.  
  133.         PUSH   BP
  134.         MOV    BP, SP
  135.         MOV    ES, [BP+6]
  136.         MOV    AX, ES:[OvrHeader.CodeSize]
  137.         MOV    Word Ptr [XmsMove.BlkSize], AX
  138.         XOR    AX, AX
  139.         MOV    Word Ptr [XmsMove.BlkSize+2], AX
  140.         MOV    AX, [OvrXmsHandle]
  141.         MOV    [XmsMove.SrcHandle], AX
  142.         MOV    AX, Word Ptr ES:[OvrHeader.XmsOffset]
  143.         MOV    Word Ptr [XmsMove.SrcOffset], AX
  144.         MOV    AX, Word Ptr ES:[OvrHeader.XmsOffset+2]
  145.         MOV    Word Ptr [XmsMove.SrcOffset+2], AX
  146.         XOR    AX, AX
  147.         MOV    [XmsMove.DestHandle], AX
  148.         MOV    Word Ptr [XmsMove.DestOffset], AX
  149.         MOV    AX, ES:[OvrHeader.LoadSeg]
  150.         MOV    Word Ptr [XmsMove.DestOffset+2], AX
  151.         MOV    AH, 11
  152.         LEA    SI, XmsMove
  153.         CALL   [XmsDriver]
  154.         OR     AX, AX
  155.         JZ     @@1
  156.         DEC    AX
  157.         JMP    @@2
  158.  
  159. @@1:    MOV    AX, ovrIOError
  160. @@2:    POP    BP
  161.         RETF   2
  162. XmsReadFunc ENDP
  163.  
  164. ;  Copy an overlaid unit from the heap to XMS
  165. ;  If successful, carry flag is cleared
  166. ;  In/Out:
  167. ;    BX:DI = offset into XMS memory block
  168.  
  169. CopyUnitToXms PROC
  170.  
  171. ;  XMS requires that an even number of bytes is moved
  172.  
  173.         MOV    DX, ES:[OvrHeader.CodeSize]
  174.         TEST   DX, 1
  175.         JZ     @@1
  176.         INC    DX
  177.         INC    ES:[OvrHeader.CodeSize]
  178.  
  179. ;  Get the fields of the XMS block move structure
  180.  
  181. @@1:    MOV    Word Ptr [XmsMove.BlkSize], DX
  182.         XOR    AX, AX
  183.         MOV    Word Ptr [XmsMove.BlkSize+2], AX
  184.         MOV    [XmsMove.SrcHandle], AX
  185.         MOV    Word Ptr [XmsMove.SrcOffset], AX
  186.         MOV    AX, [OvrHeapOrg]
  187.         MOV    Word Ptr [XmsMove.SrcOffset+2], AX
  188.         MOV    AX, [OvrXmsHandle]
  189.         MOV    [XmsMove.DestHandle], AX
  190.         MOV    Word Ptr [XmsMove.DestOffset], DI
  191.         MOV    Word Ptr [XmsMove.DestOffset+2], BX
  192.         MOV    AH, 11
  193.         LEA    SI, XmsMove
  194.  
  195. ; BUG Fix. Need to preserve BX
  196.         PUSH   BX
  197.  
  198.         CALL   [XmsDriver]
  199.  
  200. ; BUG Fix. Restore BX
  201.         POP    BX
  202.  
  203. ;  Bump code size
  204.  
  205.         ADD    DI, DX
  206.         ADC    BX, 0
  207.  
  208. ;  Check return code from XMS driver
  209.  
  210.         OR     AX, AX
  211.         JZ     @@2
  212.         CLC
  213.         RETN
  214.  
  215. @@2:    STC
  216.         RETN
  217. CopyUnitToXms ENDP
  218.  
  219. OvrXmsLoad PROC
  220.         PUSH   BP
  221.         MOV    BP, SP
  222.  
  223. ;  Walk the CodeList chain
  224. ;  First segment is PrefixSeg+10h+OvrCodeList
  225. ;  Push each element of overlaid unit list on the stack
  226. ;  Keep the size of the linked list in CX
  227.  
  228.         MOV    AX, [OvrCodeList]
  229.         XOR    CX, CX
  230. @@1:    ADD    AX, [PrefixSeg]
  231.         ADD    AX, 10h
  232.         MOV    ES, AX
  233.         PUSH   AX
  234.         INC    CX
  235.         MOV    AX, ES:[OvrHeader.CodeListNext]
  236.         OR     AX, AX
  237.         JNZ    @@1
  238.  
  239. ;  Loop:
  240. ;    Pop each element of the overlaid unit list from the stack
  241.  
  242.         XOR    BX, BX
  243.         XOR    DI, DI
  244. @@2:    POP    ES
  245.         PUSH   CX
  246.         MOV    AX, [OvrHeapOrg]
  247.         MOV    ES:[OvrHeader.LoadSeg], AX
  248.         MOV    Word Ptr ES:[OvrHeader.XmsOffset+2], BX
  249.         MOV    Word Ptr ES:[OvrHeader.XmsOffset], DI
  250.  
  251. ;  Load overlay from disk
  252.  
  253.         PUSH   BX
  254.         PUSH   DI
  255.         PUSH   ES
  256.         PUSH   ES
  257.         CALL   [OvrReadBuf]
  258.         POP    ES
  259.         POP    DI
  260.         POP    BX
  261.  
  262. ;  Flag unit as 'unloaded'; check return code
  263.  
  264.         MOV    ES:[OvrHeader.LoadSeg], 0
  265.         NEG    AX
  266.         JC     @@3
  267.  
  268.         CALL   CopyUnitToXms
  269.         JC     @@3
  270.  
  271.         POP    CX
  272.         LOOP   @@2
  273.  
  274. @@3:    MOV    SP, BP
  275.         POP    BP
  276.         RETN
  277. OvrXMSLoad ENDP
  278.  
  279. OvrInitXMS PROC
  280.  
  281. ;  Make sure the file's been opened
  282.  
  283.         XOR    AX, AX
  284.         CMP    AX, [OvrDOSHandle]
  285.         JNE    @@1
  286.         DEC    AX                      ; ovrError
  287.         JMP    @@5
  288.  
  289. ;  Check presence of XMS driver
  290.  
  291. @@1:    MOV    AX, 4300h
  292.         INT    2Fh
  293.         CMP    AL, 80h
  294.         JE     @@2
  295.         MOV    AX, ovrNoXmsDriver
  296.         JMP    @@5
  297.  
  298. ;  Get XMS driver's entry point
  299.  
  300. @@2:    MOV    AX, 4310h
  301.         INT    2Fh
  302.         MOV    Word Ptr [XmsDriver], BX
  303.         MOV    Word Ptr [XmsDriver+2], ES
  304.         CALL   AllocateXms
  305.         JNZ    @@3
  306.         MOV    AX, ovrNoXMSMemory
  307.         JMP    @@5
  308.  
  309. ;  Load the overlay into XMS
  310.  
  311. @@3:    CALL   OvrXmsLoad
  312.         JNC    @@4
  313.  
  314. ;  An error occurred.  Release handle and XMS memory
  315.  
  316.